package com.swf.jpa.interceptor;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.swf.jpa.framework.constant.MockConstants;
import com.swf.jpa.framework.constant.MockEnum;
import com.swf.jpa.generator.TableModel;
import com.swf.jpa.model.NotFoundTableModel;
import com.swf.jpa.util.MockUtils;

public class CommonInterceptor implements Interceptor {
	private static final String INNER_ERROR = "内部系统异常";
	@Override
	public boolean preIntercept(HttpServletRequest request,
			HttpServletResponse response) throws IOException, Exception {
		request.setCharacterEncoding(MockConstants.CURRENT_CHARSET);
		response.setCharacterEncoding(MockConstants.CURRENT_CHARSET);
		Map<String,String> requestMap = MockUtils.getParameterMap(request);
		TreeMap<String, String> treeMap = new TreeMap<String, String>();
		treeMap.putAll(requestMap);
		MockUtils.setAttribute(request, MockEnum.FIELDS_REQUEST, treeMap);
		MockUtils.setAttribute(request, MockEnum.ISJSON, "1".equals(requestMap.get("isJson")));
		return true;
	}
	@Override
	public void postIntercept(HttpServletRequest request,
			HttpServletResponse response) throws IOException, Exception {
		String text = "内部系统异常";
		NotFoundTableModel errorinfo = MockUtils.getAttribute(request, MockEnum.RESULT_ERROR_INFO,NotFoundTableModel.class);
		if (errorinfo!=null){
			System.out.println(errorinfo.toString());
		}
		TableModel singleReuslt = MockUtils.getAttribute(request, MockEnum.SINGLERESULT,TableModel.class);
		if (singleReuslt ==null){
			List<TableModel> listResult = MockUtils.getAttribute(request, MockEnum.LISTESULT,List.class);
			if (listResult !=null){
				text = listResult.toString();
			}
		}else{
			
			text = singleReuslt.toString();
		}
		System.out.println(text);
		
	}

}
